package com.songtech.uitest.module.common;

import com.songtech.uitest.resources.AssistResource;
import com.songtech.uitest.resources.ServerResource;
import com.songtech.uitest.resources.ThirdResource;
import com.songtech.uitest.util.AutoWait;
import com.songtech.uitest.util.Space;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;


@Component
public class LogInAndOutHandler {

    @Autowired
    @Qualifier("serverResource")
    private ServerResource serverResource;

    @Autowired
    @Qualifier("assistResource")
    private AssistResource assistResource;

    @Autowired
    @Qualifier("thirdResource")
    private ThirdResource thirdResource;


    private Logger logger = LoggerFactory.getLogger(LogInAndOutHandler.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    /**
     * 根据指定的账户号登录系统
     * @param driver
     * @param accountNum 账户编号，0为主用户，正整数为辅助账号, 9为第三方账号
     * @throws Exception
     */
    public void login(WebDriver driver, int accountNum) throws Exception{

        //先作登录数据准备
        String loginPath = "";
        String loginName = "";
        String password = "";
        String loginProvince = "";
        if(accountNum == 0){ //主账号
            loginPath = serverResource.getLoginPath();
            loginName = serverResource.getLoginName();
            password = serverResource.getPassword();
            loginProvince = serverResource.getLoginProvince();
        }
        else if(accountNum == 9){
            loginPath = thirdResource.getLoginPath();
            loginName = thirdResource.getLoginName();
            password = thirdResource.getPassword();
            loginProvince = thirdResource.getLoginProvince();
        }
        else{
            loginName = assistResource.getLoginName(accountNum);
            loginPath = assistResource.getLoginPath(accountNum);
            password = assistResource.getPassword(accountNum);
            loginProvince = assistResource.getLoginProvince(accountNum);

        }

        AutoWait.setWebDriverWait(driver);

        //再登录
//        driver.manage().window().maximize();
        driver.get(loginPath);
//        Thread.sleep(2000);
        if(accountNum != 9) { //三方门户的登录没有这个页面
//            driver.findElement(By.cssSelector("div.left")).click();
            AutoWait.findByCss("div.left").click();
        }
        //Thread.sleep(5000);//////////////////////////////////////////////////////////////////////////////////////
        //判断是否是目标省，若不是则选择省
//        String province = driver.findElement(By.id("area_name")).getText();
        String province = AutoWait.findByID("area_name").getText();
        if(!province.equals(loginProvince)){
//            driver.findElement(By.id("area")).click();
//            driver.findElement(By.xpath(".//span[text()='" + loginProvince + "']")).click();
            AutoWait.findByID("area").click();
            AutoWait.findByXpath(".//span[text()='" + loginProvince + "']").click();
        }
        //输入用户名密码登录
        driver.findElement(By.id("userLoginname")).clear();
        driver.findElement(By.id("userLoginname")).sendKeys(loginName);
        driver.findElement(By.id("userPassword")).clear();
        driver.findElement(By.id("userPassword")).sendKeys(password);
        driver.findElement(By.id("login")).click();

//        Thread.sleep(7000);
        logger.info(space.getSpace() + loginProvince + "账号" + loginName + "登录系统成功");

    }

    public void logout(WebDriver driver) throws Exception{

        AutoWait.setWebDriverWait(driver);
//        Thread.sleep(2000);
        driver.switchTo().defaultContent();
        driver.findElement(By.cssSelector("div.exit.first")).click();
//        Thread.sleep(1000);
//        driver.findElement(By.id("signoutAccount")).click();
        AutoWait.findByID("signoutAccount").click();
        logger.info(space.getSpace() + "当前账号已成功退出系统");
    }

}
